package net.zkbc.helloworld.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import net.zkbc.helloworld.service.JspFunctions;
import net.zkbc.shiro.service.ShiroUserService;

@Component(JspFunctionsImpl.INSTNAME)
public class JspFunctionsImpl implements JspFunctions {

	public static final String INSTNAME = "JSPFN";

	private static Logger LOG = LoggerFactory.getLogger(JspFunctionsImpl.class);

	@Autowired
	private ShiroUserService userService;

	private ObjectMapper objectMapper = new ObjectMapper();

	@Override
	public String toJSON(Object obj) {
		return toJSON(obj, objectMapper);
	}

	@Override
	public Object getLoginUser() {
		return userService.getLoginUser();
	}

	private String toJSON(Object obj, ObjectMapper mapper) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}

		return "";
	}

}
